/**
 * SpringMVC restful 风格
 * 客服中心控制器，控制关于客服中心的一些业务逻辑操作
 * @author yanwl
 * @date 2014-08-20
 */
package com.chnbst.bstyun.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.CommonQuestion;
import com.chnbst.bstyun.model.pojo.CustomerService;
import com.chnbst.bstyun.model.pojo.Menu;
import com.chnbst.bstyun.model.pojo.OtherFile;
import com.chnbst.bstyun.service.CommonQuestionService;
import com.chnbst.bstyun.service.CustomerServiceService;
import com.chnbst.bstyun.service.MenuService;
import com.chnbst.bstyun.service.OtherFileService;

@Controller
@RequestMapping("/service")
public class ServiceController {
	/**
	 * 页数
	 */
	private int page = 1;
	/**
	 * 常见问题每页显示的条数
	 */
	private int commonQuestionPageSize = 10;
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private MenuService menuService;
	@Autowired
	private CustomerServiceService customerServiceService;
	@Autowired
	private CommonQuestionService commonQuestionService;
	@Autowired
	private OtherFileService otherFileService;
	
	/**
	 * http://xxx.com/service
	 * get请求 返回客服中心首页
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView service(){
		List<Menu> menus = menuService.selectMenus();
		
		/**
		 * 查询所有的客服中心
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		List<CustomerService> cses = customerServiceService.selectCustomerServices(map);
		
		CustomerService cService = customerServiceService.selectCustomerServiceByCustomerServiceId(3);
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.addObject("cses", cses);
		mav.addObject("cService", cService);
		mav.setViewName("cms/service");
		return mav;
	}
	
	/**
	 * http://xxx.com/service/ktzn
	 * get请求 返回开通指南页面
	 */
	@RequestMapping(value="/ktzn/{customerServiceId}",method=RequestMethod.GET)
	public ModelAndView ktzn(@PathVariable Integer customerServiceId){
		List<Menu> menus = menuService.selectMenus();
		
		/**
		 * 查询所有的客服中心
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		List<CustomerService> cses = customerServiceService.selectCustomerServices(map);
		
		CustomerService cService = customerServiceService.selectCustomerServiceByCustomerServiceId(customerServiceId);
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("cses", cses);
		mav.addObject("menus", menus);
		mav.addObject("cService", cService);
		mav.setViewName("cms/ktzn");
		return mav;
	}
	
	/**
	 * http://xxx.com/service/czzn
	 * get请求 返回开通指南页面
	 */
	@RequestMapping(value="/czzn/{customerServiceId}",method=RequestMethod.GET)
	public ModelAndView czzn(@PathVariable Integer customerServiceId){
		List<Menu> menus = menuService.selectMenus();
		
		/**
		 * 查询所有的客服中心
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		List<CustomerService> cses = customerServiceService.selectCustomerServices(map);
		
		CustomerService cService = customerServiceService.selectCustomerServiceByCustomerServiceId(customerServiceId);
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("cses", cses);
		mav.addObject("cService", cService);
		mav.addObject("menus", menus);
		mav.setViewName("cms/czzn");
		return mav;
	}
	
	/**
	 * http://xxx.com/service/cjwtjd
	 * get请求 返回常见问题解答页面
	 */
	@RequestMapping(value="/cjwtjd",method=RequestMethod.GET)
	public ModelAndView cjwtjd(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		
		/**
		 * 查询所有的客服中心
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		List<CustomerService> cses = customerServiceService.selectCustomerServices(map);
		
		/**
		 * 常见问题总记录数
		 */
		int totalRows = commonQuestionService.selectCommonQuestionCount();
		
		String pg = request.getParameter("page");
		if(SignUtil.isNotEmpty(pg)) {
			page = Integer.valueOf(pg);
		}else {
			page = 1;
		}
		
		/**
		 * 常见问题总页数
		 */
		int pageCount = Pager.getPageCount(totalRows, commonQuestionPageSize);
		if(pageCount < page) {
			page = pageCount;
		}
		
		/**
		 * 当前页开始下标
		 */
		int start = Pager.getStart(page, commonQuestionPageSize);
		
		/**
		 * 分页查询常见问题
		 */
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("start", start);
		map1.put("pageSize", commonQuestionPageSize);
		List<CommonQuestion> cqes = commonQuestionService.selectCommonQuestions(map1);
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("cses", cses);
		mav.addObject("menus", menus);
		mav.addObject("pageCount", pageCount);
		mav.addObject("totalRows", totalRows);
		mav.addObject("page", page);
		mav.addObject("cqes", cqes);
		mav.setViewName("cms/question");
		return mav;
	}
	
	/**
	 * ajax请求查询常见问题解答
	 * @param request
	 * @return 常见问题解答页面上的数据集合Map
	 */
	@RequestMapping(value="/cjwtjd/ajaxPage",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> ajaxPage(HttpServletRequest request){
		
		/**
		 * 常见问题总记录数
		 */
		int totalRows = commonQuestionService.selectCommonQuestionCount();
		
		String pg = request.getParameter("page");
		if(SignUtil.isNotEmpty(pg)) {
			page = Integer.valueOf(pg);
		}else {
			page = 1;
		}
		
		/**
		 * 常见问题总页数
		 */
		int pageCount = Pager.getPageCount(totalRows, commonQuestionPageSize);
		if(pageCount < page) {
			page = pageCount;
		}
		
		/**
		 * 当前页开始下标
		 */
		int start = Pager.getStart(page, commonQuestionPageSize);
		
		/**
		 * 分页查询常见问题
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", start);
		map.put("pageSize", commonQuestionPageSize);
		List<CommonQuestion> cqes = commonQuestionService.selectCommonQuestions(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("page", page);
		resultMap.put("cqes", cqes);
		
		return resultMap;
	}
	
	/**
	 * http://xxx.com/service/cjwtjd/cjwtjdxq
	 * get请求 返回常见问题解答详情页面
	 */
	@RequestMapping(value="/cjwtjd/cjwtjdxq/{commonQuestionId}",method=RequestMethod.GET)
	public ModelAndView cjwtjdxq(@PathVariable Integer commonQuestionId){
		List<Menu> menus = menuService.selectMenus();
		/**
		 * 查询所有的客服中心
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		List<CustomerService> cses = customerServiceService.selectCustomerServices(map);
		
		CommonQuestion cQuestion = commonQuestionService.selectCommonQuestionByCommonQuestionId(commonQuestionId);
		
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("cses", cses);
		mav.addObject("menus", menus);
		mav.addObject("cQuestion", cQuestion);
		mav.setViewName("cms/question_detail");
		return mav;
	}
	
	/**
	 * http://xxx.com/service/cjwtjd/cjwtjdxq
	 * get请求 返回忘记账户名解答详情页面
	 * @param title
	 * @return 常见问题解答详情页面
	 */
	@RequestMapping(value="/cjwtjd/dlcjwtjdxq/{title}",method=RequestMethod.GET)
	public ModelAndView selectQuestionByTitle(@PathVariable String title){
		List<Menu> menus = menuService.selectMenus();
		/**
		 * 查询所有的客服中心
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		List<CustomerService> cses = customerServiceService.selectCustomerServices(map);
		CommonQuestion cQuestion = commonQuestionService.selectQuestionByTitle(title);
		ModelAndView mav = new ModelAndView();
		mav.addObject("cses", cses);
		mav.addObject("menus", menus);
		mav.addObject("cQuestion", cQuestion);
		mav.setViewName("cms/question_detail");
		return  mav;
	}
	
	/**
	 * http://xxx.com/service/qtdown
	 * get请求 返回其他下载页面
	 */
	@RequestMapping(value="/qtdown",method=RequestMethod.GET)
	public ModelAndView qtdown(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		
		/**
		 * 查询所有的客服中心
		 */
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", 0);
		map.put("pageSize", Integer.MAX_VALUE);
		List<CustomerService> cses = customerServiceService.selectCustomerServices(map);
		List<OtherFile> otherFiles = otherFileService.getOtherFile();
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("menus", menus);
		mav.addObject("otherFiles",otherFiles);
		mav.addObject("cses", cses);
		mav.setViewName("cms/others_down");
		return mav;
	}
}
